package com.ttjj.entity.player;

import com.alibaba.fastjson.annotation.JSONField;
import com.ttjj.common.Constants;
import com.ttjj.entity.map.Coordinate;
import com.ttjj.enums.EnumAnimalType;
import com.ttjj.enums.EnumDirection;
import com.ttjj.enums.EnumPropType;
import com.ttjj.service.navigation.TerrainAnalyzer;
import lombok.Data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 农场主（机器人/玩家）的状态
 */
@Data
public class PlayerStateVo {
    /**
     * 农场主ID
     */
    @JSONField(ordinal = 1)
    protected String playerId;
    /**
     * 农场主中心点在地图上所处的像素X位置
     */
    @JSONField(ordinal = 2)
    protected int x;
    /**
     * 农场主中心点在地图上所处的像素Y位置
     */
    @JSONField(ordinal = 3)
    protected int y;
    /**
     * 农场主宽度，默认为50
     */
    @JSONField(ordinal = 4)
    protected int width;
    /**
     * 农场主高度，默认为50
     */
    @JSONField(ordinal = 5)
    protected int height;
    /**
     * 农场主朝向
     */
    @JSONField(ordinal = 6)
    protected EnumDirection movingDirect;
    /**
     * 农场主当前实际速度
     */
    @JSONField(ordinal = 7)
    protected int speed;
    /**
     * 农场主当前背包携带容量
     */
    @JSONField(ordinal = 8)
    protected int carrySize;
    /**
     * 已交付动物类型
     */
    @JSONField(ordinal = 9)
    protected List<EnumAnimalType> delivered = new ArrayList();
    /**
     * 正在携带的动物类型
     */
    @JSONField(ordinal = 10)
    protected List<EnumAnimalType> carried = new ArrayList();
    /**
     * 当前持有的道具类型
     */
    @JSONField(ordinal = 11)
    protected List<EnumPropType> holdingProps = new ArrayList();
    /**
     * 是否正遭受其他农场主撞击处于眩晕状态
     */
    @JSONField(ordinal = 12)
    protected boolean impacted;
    /**
     * 队伍Id
     */
    @JSONField(ordinal = 13)
    protected String teamId;
    /**
     * 队伍颜色
     */
    @JSONField(ordinal = 14)
    protected String teamColor;
    /**
     * 玩家在本局赛事的实时排名-v1.5新增
     */
    private Integer rank;
    /**
     * 坐标位置
     */
    protected Coordinate coordinate;
    /**
     * 下一步位置
     */
    private Coordinate nextCoordinate;

    protected int radius = Constants.getCommonLen() / 2;

    /**
     * 获取当前坐标
     *
     * @return
     */
    public Coordinate getCoordinate() {
        return new Coordinate(x, y);
    }

    /**
     * 获取下一步位置
     *
     * @return
     */
    public Coordinate getNextCoordinate() {
        Coordinate coordinate = getCoordinate();
        int speedByCod = TerrainAnalyzer.getSpeedByCod(this, coordinate);
        return coordinate.stepByDirect(this.movingDirect, speedByCod);
    }

    /**
     * 获取下一步五种可能移动选择后的位置
     *
     * @return
     */
    public List<Coordinate> getAllNextCoordinate() {
        List<Coordinate> nextCoordinates = new ArrayList<>();
        List<EnumDirection> allDirections = Arrays.stream(EnumDirection.values()).collect(Collectors.toList());

        Coordinate coordinate = getCoordinate();
        int speedByCod = TerrainAnalyzer.getSpeedByCod(this, coordinate);
        for (EnumDirection direction: allDirections) {
            nextCoordinates.add(coordinate.stepByDirect(direction, speedByCod));
        }

        return nextCoordinates;
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        } else if (obj != null && this.getClass() == obj.getClass()) {
            PlayerStateVo other = (PlayerStateVo) obj;
            return this.hashCode() == other.hashCode();
        } else {
            return false;
        }
    }

    public int hashCode() {
        return this.playerId.hashCode();
    }
}
